//
//  WYCCreditCardController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/12/25.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCDebitCardController.h"
#import "WYCDebitModel.h"
#import "WYCDebitFrameModel.h"
#import "WYCDebitCell.h"

@interface WYCDebitCardController ()
/** 列表数组*/
@property(nonatomic,strong)NSMutableArray * billArray;
@end

@implementation WYCDebitCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = WYCGlobalBg;

    self.billArray = [[NSMutableArray alloc] init];
    
    //    [self setupRuqst];
    
    [self setupTableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRuqst)];
    [self.tableView.mj_header beginRefreshing];
}
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self.tableView.mj_header beginRefreshing];
//
//}

- (void)setupTableView
{
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = NavHeight;
    self.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    self.tableView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    self.tableView.sectionHeaderHeight = 220 * px;
    self.tableView.rowHeight = 150 * px;
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置滚动条的内边距
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (void)setupRuqst {
    [self.billArray removeAllObjects];
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",usergetDebitCard,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                
                WYCDebitModel * model = [WYCDebitModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{
                    WYCDebitFrameModel * BillFrameModel = [[WYCDebitFrameModel alloc] init];
                    BillFrameModel.Billmodel = model;
                    [self.billArray addObject:BillFrameModel];
                    [self.delegate setdebit:[NSString stringWithFormat:@"%lu",(long)self.billArray.count]];
                }
            }
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.billArray.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull WYCDebitCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    cell.layer.zPosition = indexPath.row;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WYCDebitFrameModel * model = self.billArray[indexPath.row];
    
    return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WYCDebitCell * cell = [WYCDebitCell cellWithTableView:tableView];
    cell.billmodelFrame = self.billArray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    return cell;
}

@end

